/**
 * Copyright(c) eking Technology Co., LTD.
 *
 * @date 2013年8月7日
 * 	
 * Original Author: 郑兴(zhengxing)
 *
 * ChangeLog
 *
 */
package com.hnas.sys.core.util;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.hnas.core.exception.CommonException;
import com.hnas.core.util.StringUtil;
import com.hnas.sys.service.system.dto.DTOAuthority;
import com.hnas.sys.service.system.dto.DTOUser;

/**
 * 
 * @author 郑兴(zhengxing)
 *
 */
public class UserUtils {
	public final static String MIX_FRONT_LOGIN_USER = "MIX_FRONT_LOGIN_USER";
	public final static String MIX_LAST_SENT_SMS = "MIX_LAST_SENT_SMS";
	public final static String MIX_SMS_CODE = "MIX_SMS_CODE";
	public final static String MIX_SMS_CODE_BACK = "MIX_SMS_CODE_BACK";
	public final static String MIX_IMG_CODE = "authCode";
	public final static String MIX_RECENT_MOBILE = "MIX_RECENT_MOBILE";
	public final static String MIX_LAST_VISIT_ORDER = "MIX_LAST_VISIT_ORDER";
	public final static String MIX_CART_ID = "MIX_CART_ID";//购物车ID
	public final static String MIX_FRONT_FLIGHT_SEARCH = "MIX_FRONT_FLIGHT_SEARCH";//查询条件

	public static DTOUser getCurrentUser() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if(null == authentication || null == authentication.getPrincipal()) {
			return null;
		}
		if (authentication.getPrincipal() instanceof DTOUser)		
			return (DTOUser) authentication.getPrincipal();
		return null;
	}
	public static DTOUser getFrontUser() {
		HttpServletRequest request = UserUtils.getServletRequest();
		if (null!=request){
			DTOUser user = (DTOUser)request.getSession(true).getAttribute(MIX_FRONT_LOGIN_USER);
			if (null==user)
				return null;
			return user;
		}
		return null;
	}

	public static String getLastVisitOrder(){
		HttpServletRequest request = UserUtils.getServletRequest();
		if (null!=request){
			return (String)request.getSession(true).getAttribute(MIX_LAST_VISIT_ORDER);
		}
		return null;
	}
	public static void setLastVisitOrder(String orderID) throws CommonException{
		HttpServletRequest request = UserUtils.getServletRequest();
		if (null!=request){
			request.getSession(true).setAttribute(MIX_LAST_VISIT_ORDER,orderID);
		}else{
			throw new CommonException("没有找到上下文！");
		}
	}
	public static void setMyCart(long cartId) throws CommonException{
		HttpServletRequest request = UserUtils.getServletRequest();
		if (null!=request){
			request.getSession(true).setAttribute(MIX_CART_ID,cartId);
		}else{
			throw new CommonException("没有找到上下文！");
		}
	}
//	public static void setSearchCon(DTOSearchFlight dto) throws CommonException{
//		HttpServletRequest request = UserUtils.getServletRequest();
//		if (null!=request){
//			request.getSession(true).setAttribute(MIX_FRONT_FLIGHT_SEARCH,dto);
//		}else{
//			throw new CommonException("没有找到上下文！");
//		}
//	}
//	public static DTOSearchFlight getSearchCon(){
//		HttpServletRequest request = UserUtils.getServletRequest();
//		if (null!=request){
//			return (DTOSearchFlight)request.getSession(true).getAttribute(MIX_FRONT_FLIGHT_SEARCH);
//		}
//		return null;
//	}
	public static Long getMyCartId(){
		HttpServletRequest request = UserUtils.getServletRequest();
		if (null!=request){
			return (Long)request.getSession(true).getAttribute(MIX_CART_ID);
		}
		return null;
	}
	public static boolean checkSmsCode(String mobile, String authCode) throws CommonException{
		HttpServletRequest request = UserUtils.getServletRequest();
		if (null==request){
			throw new CommonException("没有上下文！");
		}
		if (StringUtil.isEmpty(mobile)){
			throw new CommonException("请输入预订手机！");
		}
		if (StringUtil.isEmpty(authCode)){
			throw new CommonException("请输入短信验证码！");
		}
		if (!mobile.equals(request.getSession(true).getAttribute(MIX_RECENT_MOBILE))){
			throw new CommonException("预订手机号和发送短信验证码的手机号不一致！");
		}
		if (!authCode.equals(request.getSession(true).getAttribute(MIX_SMS_CODE))){
			throw new CommonException("短信验证码不正确！");
		}
		request.getSession(true).setAttribute(MIX_RECENT_MOBILE,mobile);
//		request.getSession(true).setAttribute("LastUpdate",new Date());
		DTOUser user =  new DTOUser();
		user.setAccount(mobile);
		user.setTelephone(mobile);
		user.setLastVisit(new Date());
		request.getSession(true).setAttribute(MIX_FRONT_LOGIN_USER,user);
		//清空验证的短信验证码
		request.getSession(true).removeAttribute(UserUtils.MIX_SMS_CODE);
		return true;
	}
	public static void smsLogout(){
		HttpServletRequest request = UserUtils.getServletRequest();
		if (null==request)
			return;
		request.getSession(true).removeAttribute(MIX_FRONT_LOGIN_USER);
		request.getSession(true).removeAttribute(MIX_SMS_CODE);
		request.getSession(true).removeAttribute(MIX_RECENT_MOBILE);
	}
	public static HttpServletRequest getServletRequest() {
		HttpServletRequest request = null;		
		ServletRequestAttributes srs =	(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (null!=srs){
			request = srs.getRequest();
		}
//		if (request == null) {
//			request = XFireServletController.getRequest();
//		}
		return request;
	}
	

public static String getIpAddress(){
	HttpServletRequest request = UserUtils.getServletRequest();
	if (null!=request){
		return getIpAddress(request);
	}
	return null;
}
public static String getIpAddress(HttpServletRequest request ){
	String ip = request.getHeader("x-forwarded-for");      
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {         
    ip = request.getHeader("Proxy-Client-IP");      
    }   
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
    ip = request.getHeader("WL-Proxy-Client-IP");       
    }       
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {   
    ip = request.getRemoteAddr();      
    } 
	return ip;
}
	
	public static String getCurrentUserAccount() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request.getRemoteUser();
	}

	public static Authentication getAuthentication() {
		return SecurityContextHolder.getContext().getAuthentication();
	}
	
	public static boolean hasPermission(DTOUser user, String roleName){
		for (GrantedAuthority au:user.getAuthorities()){
			if ((au instanceof DTOAuthority) && roleName.equals(((DTOAuthority)au).getName()))
				return true;
		}
		return false;
	}

}
